#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年5月15日

@author: chenyongbing
'''
import sys,os,tarfile,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
from base.time_method import myTimeMethod
from base.zhimind_datas import myZhimindDatas
from base.retail_datas import myRetailDatas

from localConfig import myLocalConfig
import logging
class RssiWareForm(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
    
    def openTarFile(self,rawfile_tar):
        output = ''
        if not os.path.exists(rawfile_tar):
            return output
        try:
            tar = tarfile.open(rawfile_tar)
        except:return ''
        for member in tar.getmembers():
            f = tar.extractfile(member)
            out = f.read()
            output = output + out+'\n'
        tar.close()
        return output 
    
    
    def read_mac_rssi_from_rawdata(self,rawpath='',group=None,day=None,employees=[]):
        datas = []
        if len(employees)==0:
            comp_group  = myRetailDatas.get_comp_group( shop_group=group)
            employees = myZhimindDatas.get_employees( mtype=1,group=comp_group)
        if len(employees)==0:
            return datas
        if len(rawpath)==0 or rawpath=='':
            rawpath = myLocalConfig.rawdata_path
        
        
        

        rawfile_tar = rawpath+'/rawdata_group/'+day+'/'+group+'.txt.tar.gz'
        #print rawfile_tar
        if not os.path.exists(rawfile_tar):
            logging.warning( 'rawfile %s not exists.'%rawfile_tar)
            continue
        output = self.openTarFile(rawfile_tar)
        logging.warning( "%s,%s"%(rawfile_tar,len(output)))
        for line in output.split('\n'):
            msgLine = line.split('.')
            #print len(msgLine)
            if len(msgLine)!=6 and len(msgLine)!=5:
                continue
            if len(msgLine)==6:
                day,dev,dmac,dtime,rssi,rtime = msgLine
            elif len(msgLine)==5:
                day,dev,dmac,dtime,rssi = msgLine
            if not re.search('^-[\d]+$',rssi):continue
            if dmac not in employees:continue
            datas.append([day,dev,dmac,rssi])
        logging.warning( "found %s"%len(datas))
        return datas
    
if __name__ == '__main__':
    pass